package com.mindainfo.groundwave.server.member.dao.impl;

import com.mindainfo.groundwave.server.base.dao.impl.BaseDaoImpl;
import com.mindainfo.groundwave.server.member.bean.SysRole;
import com.mindainfo.groundwave.server.member.dao.IRoleDao;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by MD-01 on 2017/5/15.
 */
@Repository
public class RoleDaoImpl extends BaseDaoImpl<SysRole> implements IRoleDao {

    public List<SysRole> getByIds(List<Integer> roleIds){
        return getDatastore().createQuery(SysRole.class)
                .field("id").in(roleIds)
                .asList();
    }

    @Override
    public SysRole queryByName(String name) {
        return getDatastore().createQuery(SysRole.class)
                .field("name").equal(name)
                .get();
    }

    @Override
    public SysRole queryBySign(String sign) {
        return getDatastore().createQuery(SysRole.class)
                .field("sign").equal(sign)
                .get();
    }

    @Override
    public List<SysRole> queryList(int start, int num, String key) {
        Query<SysRole> query = getDatastore().createQuery(SysRole.class);
        if(!StringUtils.isEmpty(key)){
            query.field("name").contains(key);
        }
        FindOptions options = new FindOptions();
        options.skip(start).limit(num);
        return query.order("orderNum").asList(options);
    }

    @Override
    public int queryListCount(String key) {
        Query<SysRole> query = getDatastore().createQuery(SysRole.class);
        if(!StringUtils.isEmpty(key)){
            query.field("name").contains(key);
        }
        return (int) query.count();
    }

    @Override
    public List<SysRole> queryUsableAll() {
        return getDatastore().createQuery(SysRole.class)
                .field("usable").equal(true)
                .order("orderNum").asList();
    }
}
